package com.flipdf2.serialize.ppml.element;

import java.util.ArrayList;
import java.util.List;

import org.jdom.Element;
import org.jdom.Namespace;

import com.flipdf2.serialize.ppml.PPMLException;

public class PPMLOccurrenceList {
	public static final String NAME = "OCCURRENCE_LIST";
	
	private List<PPMLOccurrence> occurrenceList;
	
	public Element toXML(Namespace defaultNamespace) {
		Element root = null;
		if (defaultNamespace != null) {
			root = new Element(NAME, defaultNamespace);
		} else {
			root = new Element(NAME);	
		}
		
		if (occurrenceList != null && occurrenceList.size() > 0) {
			for (int i = 0, n = occurrenceList.size(); i < n; i++) {
				root.addContent(occurrenceList.get(i).toXML(defaultNamespace));
			}
		}
		
		return root;
	}

	public void check() throws PPMLException {
		if (occurrenceList == null) {
			throw PPMLException.getInstance(PPMLException.REQUIRED_ELEMENT_LOST, "OCCURRENCE");
		}
	}
	
	public List<PPMLOccurrence> getOccurrenceList() {
		return occurrenceList;
	}

	public void setOccurrenceList(List<PPMLOccurrence> occurrenceList) {
		this.occurrenceList = occurrenceList;
	}
	
	public void addOccurrence(PPMLOccurrence occurrence) {
		if (this.occurrenceList == null)
			this.occurrenceList = new ArrayList<PPMLOccurrence>();
		this.occurrenceList.add(occurrence);
	}
}
